package com.dqyt.khjc.service;

import com.alibaba.fastjson.JSON;
import com.dqyt.app.bean.LocaGzlqrd;
import com.dqyt.app.bean.Sample;
import com.dqyt.app.mongo.LocaGzlqrdDao;
import com.dqyt.app.mongo.SampleDao;
import com.dqyt.common.constant.Opt;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.*;
import com.dqyt.khjc.config.Config;
import com.dqyt.khjc.mapper.HistoryMapper;
import com.dqyt.khjc.swicth.SwitchConfig;
import com.dqyt.khjc.utils.IpUtil;
import com.dqyt.khjc.utils.TimeUtil;
import com.dqyt.quart.AddSimpleJobReq;
import com.dqyt.quart.DeleteJobReq;
import com.dqyt.quart.QuartzManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class LocaGzlqrdService {

    @Autowired
    private LocaGzlqrdDao locaGzlqrdDao;


    @Autowired
    private HistoryMapper historyMapper;

    @Autowired
    private QuartzManager quartzManager;

    /**
     * 新建一个 本地工作量确认单
     * @param request
     * @param locaGzlqrd
     * @param rwdbh
     * @return
     */
    public ResultMsg saveLocaGzlqrd(HttpServletRequest request, String locaGzlqrd, String rwdbh) {
        ResultMsg ret = new ResultMsg();
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值

            LocaGzlqrd locaGzlqrd1 = JSON.parseObject(locaGzlqrd, LocaGzlqrd.class);

            LocaGzlqrd locaGzlqrd2 = locaGzlqrdDao.getLocaGzlqrdById(rwdbh);
            if(null != locaGzlqrd2){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("此工作量确认单已经保存过");
                return ret;
            }
            locaGzlqrdDao.save(locaGzlqrd1);
            ret.setCode(ResultMsg.SUCESS);


            DeleteJobReq deleteJobReq = new DeleteJobReq();
            deleteJobReq.setJobName(Config.PZGONGZLQRD_TASK);
            deleteJobReq.setJobGroupName(Config.PZGONGZLQRD_TASK_GROUP);
            deleteJobReq.setTriggerName(Config.PZGONGZLQRD_TASK);
            deleteJobReq.setTriggerGroupName(Config.PZGONGZLQRD_TASK_GROUP);
            quartzManager.removeJob(deleteJobReq);


            Calendar beforeTime = Calendar.getInstance();
            // 5 秒之后的时间
            beforeTime.add(Calendar.SECOND, 5);
            Date beforeDate = beforeTime.getTime();
            AddSimpleJobReq addSimpleJobReq = new AddSimpleJobReq();
            addSimpleJobReq.setDate(beforeDate);
            addSimpleJobReq.setJobClass("PzGzlqrdTask");
            quartzManager.addSimpleJob(addSimpleJobReq, Config.PZGONGZLQRD_TASK,Config.PZGONGZLQRD_TASK_GROUP);
            return ret;
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 分页查询 样品接收单
     * @param request
     * @param pageNum
     * @param pageSize
     * @param rwdbh
     * @param wtfId
     * @return
     */
    public ResultMsg getLocaGzlqrdPage(HttpServletRequest request,Integer pageNum,Integer pageSize,String rwdbh, int wtfId,int wtfchId,int start){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        List<ReceiptTbBean> receiptTbBeanList = new ArrayList<ReceiptTbBean>();
        try {

            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            if (pageNum != null && pageSize != null) {
                pageNum = (pageNum - 1) * pageSize;
            }
            List<LocaGzlqrd>  data =  locaGzlqrdDao.getLocaGzlqrdPage(pageNum,pageSize,rwdbh,wtfId,wtfchId,start);
            long  total = locaGzlqrdDao.getLocaGzlqrdTotalNumber(rwdbh,wtfId,wtfchId,start);
            RespPageBean bean = new RespPageBean();
            bean.setData(data);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }




    /**
     * 分页查询 样品接收单   待批准的
     * @param request
     * @param pageNum
     * @param pageSize
     * @param rwdbh
     * @param wtfId
     * @return
     */
    public ResultMsg getLocaGzlqrdDpzPage(HttpServletRequest request,Integer pageNum,Integer pageSize,String rwdbh, int wtfId,int wtfchId){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        List<ReceiptTbBean> receiptTbBeanList = new ArrayList<ReceiptTbBean>();
        try {

            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            if (pageNum != null && pageSize != null) {
                pageNum = (pageNum - 1) * pageSize;
            }
            List<LocaGzlqrd>  data =  locaGzlqrdDao.getLocaGzlqrdDpzPage(pageNum,pageSize,rwdbh,wtfId,wtfchId);
            long  total = locaGzlqrdDao.getLocaGzlqrdDpzTotalNumber(rwdbh,wtfId,wtfchId);
            RespPageBean bean = new RespPageBean();
            bean.setData(data);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 设置 驳回工作量确认单
     * @param request
     * @param bhgyy
     * @return
     */
    public ResultMsg rejectQrdDal2(HttpServletRequest request,String bhgyy,String rwdbh){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            User user = UserInfoUtil.getUser_Web(request);


            LocaGzlqrd locaGzlqrd =  locaGzlqrdDao.getLocaGzlqrdById(rwdbh);
            locaGzlqrd.bhgyy = bhgyy;
            locaGzlqrd.start = 0;
            locaGzlqrd.jydwFzrId =0;
            locaGzlqrd.jydwFzr ="";
            locaGzlqrdDao.save(locaGzlqrd);


            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("操作成功");
            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添加操作记录
                // 先取得用户ip
                String ip = IpUtil.getIpAddr(request);
                //被操作者id
                String bopIdStr = JSON.toJSONString(rwdbh);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 驳回了任务单位 " + rwdbh + " 的 工作量确认单");
                historyBean.setType(Opt.BGGL_BHGZLQRD);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(bopIdStr);
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                historyMapper.addHistory(historyBean);
            }
            return ret;


        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }






    /**
     * 设置 过 通工作量确认单
     * @param request
     * @return
     */
    public ResultMsg srtQrdOkProgress2(HttpServletRequest request,String rwdbh){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            User user = UserInfoUtil.getUser_Web(request);

            String qzrq = TimeUtil.formatTime_tow(TimeUtil.currentTimeMillis());

            LocaGzlqrd locaGzlqrd =  locaGzlqrdDao.getLocaGzlqrdById(rwdbh);
            locaGzlqrd.start =2;
            locaGzlqrd.jydwFzrId =user.getUserId();
            locaGzlqrd.jydwFzr = user.getUserName();
            locaGzlqrd.qzrq =qzrq;

            locaGzlqrdDao.save(locaGzlqrd);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("操作成功");
            return ret;
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }




    /**
     * 新建一个 本地工作量确认单
     * @param request
     * @param locaGzlqrd
     * @return
     */
    public ResultMsg upLocaGzlqrd(HttpServletRequest request, String locaGzlqrd) {
        ResultMsg ret = new ResultMsg();
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            LocaGzlqrd locaGzlqrd1 = JSON.parseObject(locaGzlqrd, LocaGzlqrd.class);
            locaGzlqrdDao.save(locaGzlqrd1);
            ret.setCode(ResultMsg.SUCESS);

            DeleteJobReq deleteJobReq = new DeleteJobReq();
            deleteJobReq.setJobName(Config.PZGONGZLQRD_TASK);
            deleteJobReq.setJobGroupName(Config.PZGONGZLQRD_TASK_GROUP);
            deleteJobReq.setTriggerName(Config.PZGONGZLQRD_TASK);
            deleteJobReq.setTriggerGroupName(Config.PZGONGZLQRD_TASK_GROUP);
            quartzManager.removeJob(deleteJobReq);


            Calendar beforeTime = Calendar.getInstance();
            // 5 秒之后的时间
            beforeTime.add(Calendar.SECOND, 5);
            Date beforeDate = beforeTime.getTime();
            AddSimpleJobReq addSimpleJobReq = new AddSimpleJobReq();
            addSimpleJobReq.setDate(beforeDate);
            addSimpleJobReq.setJobClass("PzGzlqrdTask");
            quartzManager.addSimpleJob(addSimpleJobReq, Config.PZGONGZLQRD_TASK,Config.PZGONGZLQRD_TASK_GROUP);


            return ret;
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    public ResultMsg  deleteLocaGzlqrd (HttpServletRequest request,String rwdbh) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            long res = locaGzlqrdDao.deleteById(rwdbh);
            if(res>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("操作成功");
                //添加操作记录
                // 先取得用户ip
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 删除了任务单号为： [" + rwdbh + "] 的本地工作量确认单");
                historyBean.setType(Opt.RWYL_BDGZLQRD_SC);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(rwdbh);
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常");
            }


        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 分页查询 样品接收单
     * @param request
     * @param rwdbh
     * @param wtfId
     * @return
     */
    public ResultMsg getLocaGzlqrdTj(HttpServletRequest request,String rwdbh, int wtfId,int wtfchId,int start){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        List<ReceiptTbBean> receiptTbBeanList = new ArrayList<ReceiptTbBean>();
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            List<LocaGzlqrd>  data =  locaGzlqrdDao.getLocaGzlqrdTj(rwdbh,wtfId,wtfchId,start);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(data);
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


}
